const puppeteer = require('puppeteer');
const fs = require('fs')

class WebVideo {
  constructor (options) {

  }
  async init () {
    this.browser = await puppeteer.launch({
      ignoreHTTPSErrors: true,
      headless: false,
      // devtools: true
    });
    this.page = await this.browser.newPage();
    await this.openAPage('http://10.17.1.117/video2/index.html');
    setTimeout(() => {
      this.startCut()
    }, 3000);
  }
  async openAPage (url) {
    await this.page.goto(url, {
      timeout:0,
      waitUntil: ['domcontentloaded']
    })
    console.log('dom 加载完成')
    this.iframe = await this.page.$('iframe');
    // console.log('iframe',this.iframe)
    return this.iframe;
  }
  startCut () {
    this.imgIndex = 0;
    this.updateCanvas()
  }
  updateCanvas () {
    if (this.imgIndex > 150) {
      return;
    }
    let that = this
    console.log(this.imgIndex)
    this.iframe.screenshot({
      encoding: 'base64'
    }).then(base64 => {
      let buffer = Buffer.from(base64, 'base64');
      fs.writeFile(`./images2/img${this.imgIndex}.png`, buffer, (err) => {
        this.page.evaluate((data) => {
          window.chromePlayer.pause(data * 20);
        }, that.imgIndex)

        setTimeout(() => {
          this.updateCanvas(this.imgIndex++)
        }, 0)
      })
    })
  }
}
new WebVideo().init()